package cn.slipi.common.serializer;

import com.alibaba.fastjson.JSON;
import org.I0Itec.zkclient.serialize.ZkSerializer;

import java.nio.charset.StandardCharsets;

/**
 * The type FastJson serializer.
 */
public class FastJsonSerializer implements ZkSerializer {

    @Override
    public byte[] serialize(final Object o) {
        if (o == null) {
            return new byte[]{};
        }
        if (o instanceof String) {
            return o.toString().getBytes(StandardCharsets.UTF_8);
        }
        return JSON.toJSONString(o).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public Object deserialize(final byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }
}
